package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    private  final BrandService brandService;
    @Autowired
    CategoryBrandService categoryBrandService;

    public BrandController (BrandService brandService){
        this.brandService=brandService;
    }
    /**
     * 根据id查询品牌
     * @param id 品牌的id
     * @return 品牌对象
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){
        Brand byId = brandService.getById(id);
        BrandDTO brandDTO = new BrandDTO(byId);
        return ResponseEntity.ok(brandDTO);
    }

    /**
     * 根据品牌id集合查询品牌集合
     * @param idList id集合
     * @return BrandDTO 集合
     */
    @GetMapping("list")
    public  ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long>ids){
        List<Brand> brands = brandService.listByIds(ids);
        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(brands);
        return ResponseEntity.ok(brandDTOS);
    }
/**
 *
 * @param page 当前页码
 * @param rows 每页大小
 * @param key 查询条件
 * @return 品牌分页结果
 */
        @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                              @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                              @RequestParam(value = "key",required = false)String key){
        PageDTO<BrandDTO> pageDTO=brandService.queryBrandByPage(page,rows,key);
        return ResponseEntity.ok(pageDTO);

    }
    /**
     * 根据分类查询品牌
     * @return 品牌集合
     */
    @GetMapping("/of/category/")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long id){
        return ResponseEntity.ok(brandService.queryBrandByCategory(id));
    }
    /**
     * 修改品牌
     * @param brandDTO 品牌及分类信息
     * @return 无
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 删除品牌
     * @param id 要删除的品牌id
     * @return 无
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> delectBrandById(@PathVariable("id")Long id){
//        //先删除中间表
//        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",id));
//        //删除品牌表
//        brandService.removeById(id);
//        //返回
        brandService.delectBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
//    - 请求方式：`PUT`
//- 请求路径：`/brand`
//- 请求参数：BrandDTO对象
    @PutMapping("brand")
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
